{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Segments in a String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countSegments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串中的单词数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>统计字符串中的单词个数，这里的单词指的是连续的不是空格的字符。</p>\n",
    "\n",
    "<p>请注意，你可以假定字符串里不包括任何不可打印的字符。</p>\n",
    "\n",
    "<p><strong>示例:</strong></p>\n",
    "\n",
    "<pre><strong>输入:</strong> &quot;Hello, my name is John&quot;\n",
    "<strong>输出:</strong> 5\n",
    "<strong>解释: </strong>这里的单词是指连续的不是空格的字符，所以 &quot;Hello,&quot; 算作 1 个单词。\n",
    "</pre>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-segments-in-a-string](https://leetcode.cn/problems/number-of-segments-in-a-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-segments-in-a-string](https://leetcode.cn/problems/number-of-segments-in-a-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"Hello, my name is John\"', '\"Hello\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        res = s.split(' ')\n",
    "        return len(res) if all(res) else len(res)-res.count('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i] == ' ':\n",
    "                i += 1\n",
    "                continue\n",
    "            while i < len(s) and s[i] != ' ':\n",
    "                i += 1\n",
    "            ans += 1\n",
    "        return ans\n",
    "\n",
    "        \n",
    "            \n",
    "\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "\n",
    "        if len(s) ==0 or s[0] == ' ':\n",
    "            ans=0\n",
    "        else:\n",
    "            ans = 1    \n",
    "        for i in range(len(s)-1):\n",
    "            if s[i] == ' ' and s[i+1] != ' ':\n",
    "                ans+=1\n",
    "        return ans        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        n=len(s)\n",
    "        ans=0;\n",
    "        for i in range(n):\n",
    "            if (i==0 or s[i-1]==' ') and s[i]!=' ':\n",
    "                ans+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        l = s.split(' ')\n",
    "        return len(l) - l.count('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        lt=s.split(' ')\n",
    "        res=0\n",
    "        for i in range(len(lt)):\n",
    "            if lt[i]!='':\n",
    "                res+=1\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if not s.strip():\n",
    "            return 0\n",
    "        return len(re.split(\" +\", s.strip()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return sum(st != \"\" for st in s.split(\" \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def countSegments(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        return sum(bool(word) for word in s.split())\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(s)):\n",
    "            if s[i] != ' ' and (i == 0 or s[i - 1] == ' '):\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        lsts = s.split()\n",
    "        return len(lsts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        ans, i, k = 0, 0, 0\n",
    "        while i < len(s):\n",
    "            if s[i] == \" \":\n",
    "                k = 0\n",
    "            else:\n",
    "                if k == 0:\n",
    "                    ans += 1 \n",
    "                k = 1\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int: \n",
    "        a = s.split(\" \")\n",
    "        res = len(a)\n",
    "        for i in a:\n",
    "            if i == \"\":\n",
    "                res -= 1\n",
    "        return res\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s):\n",
    "        segment_count = 0\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if (i == 0 or s[i - 1] == ' ') and s[i] != ' ':\n",
    "                segment_count += 1\n",
    "\n",
    "        return segment_count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(s)):\n",
    "            if (s[i-1] == ' ' or i == 0) and s[i] != ' ':\n",
    "                count += 1\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if len(s) == 0:\n",
    "            return 0\n",
    "        count = 0\n",
    "        for i in range(len(s)):\n",
    "            if i == len(s) - 1:\n",
    "                break\n",
    "            if s[i] != ' ' and s[i+1] == ' ':\n",
    "                count = count + 1\n",
    "        if s[-1] != ' ':\n",
    "            count = count + 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        '''\n",
    "        res=0\n",
    "        i=0#指针0开始往后\n",
    "        while i<len(s): \n",
    "          if s[i]==' ':\n",
    "             i+=1#遇到空格就跳过\n",
    "             continue\n",
    "          if s[i]!=' ':\n",
    "             i+=1#往后继续遍历\n",
    "          res+=1#统计没有隔断的个数\n",
    "        return res\n",
    "        '''\n",
    "        return len(s.split())\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        ss = s.split()\n",
    "        return(len(ss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        \n",
    "        # 解法1\n",
    "        count = 0\n",
    "        # 如果当前字符不是空格且前一个字符是空格（\n",
    "        # 或者当前字符是第一个字符），则认为是新的单词的起始\n",
    "        for i in range(0,len(s)):\n",
    "            if (i == 0 or s[i - 1] == ' ') and s[i] != ' ':\n",
    "                count += 1\n",
    "\n",
    "        return count \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "        # 错的的解答\n",
    "        # new = s.split()\n",
    "        # print(len(new))\n",
    "        # return len(new)\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def countSegments(self, s: str) -> int:\r\n",
    "        if not s: return 0\r\n",
    "        res = 0\r\n",
    "        i = 0\r\n",
    "        while i < len(s):\r\n",
    "            if s[i] == ' ':\r\n",
    "                i += 1\r\n",
    "                continue\r\n",
    "            while i < len(s) and s[i] != ' ':\r\n",
    "                i += 1\r\n",
    "            res += 1\r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "\n",
    "        \n",
    "        return len(list(filter(lambda l:len(l)>0, s.split(\" \"))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        s_len=s.split()\n",
    "        return len(s_len)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if s == \"\":  # 考虑 s3 的情况\n",
    "            return 0\n",
    "        count = 1 if s[0] != \" \" else 0  # 考虑开头为空格的情况(s6)\n",
    "        for i, c in enumerate(s):\n",
    "            if c == \" \" and i+1 < len(s) and s[i+1] != \" \":  # 注意考虑 s5 的情况\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return sum(st != \"\" for st in s.split(\" \"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s):\n",
    "        segment_count = 0\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if (i == 0 or s[i - 1] == ' ') and s[i] != ' ':\n",
    "                segment_count += 1\n",
    "\n",
    "        return segment_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        n = s.split(' ')\n",
    "        flag = 0\n",
    "        for i in n:\n",
    "            if i != '':\n",
    "                flag = flag + 1\n",
    "        return flag\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        # if s=='':\n",
    "        #     return 0\n",
    "        li=s.split(' ')\n",
    "        count=0\n",
    "        for item in li:\n",
    "            if item!='' and item!=' ':\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        # l = list(s.split(' '))\n",
    "        # return(len(l))\n",
    "        ans = []\n",
    "        t = ''\n",
    "        for i in range(0, len(s), 1):\n",
    "            if s[i] == ' ':\n",
    "                if len(t) > 0:\n",
    "                    ans.append(t)\n",
    "                t = ''\n",
    "            else:\n",
    "                t += s[i]\n",
    "        if len(t) != 0:\n",
    "            ans.append(t)    \n",
    "        return (len(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        c = 0\n",
    "        pre = True\n",
    "        for i in s:\n",
    "            if i != \" \" and pre:\n",
    "                c += 1\n",
    "            pre = i==\" \"\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        ls = s.split()\n",
    "        return len(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if not s.strip():\n",
    "            return 0\n",
    "        return len([w for w in s.split(' ') if w.strip()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        ss=s.split(' ')\n",
    "        ss=[x for x in ss if x !='']\n",
    "        return len(ss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if all(c == \" \" for c in s):  # 考虑 s3, s4 的情况\n",
    "            return 0\n",
    "        count = 1 if s[0] != \" \" else 0  # 考虑开头为空格的情况(s6)\n",
    "        for i, c in enumerate(s):\n",
    "            if c == \" \" and i+1 < len(s) and s[i+1] != \" \":  # 注意考虑 s5 的情况\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        # 法一：\n",
    "        # if s == \"\":  # 考虑 s3 的情况\n",
    "        #     return 0\n",
    "        # count = 1 if s[0] != \" \" else 0  # 考虑 s6 开头为空格的情况\n",
    "        # for i, c in enumerate(s):\n",
    "        #     if c == \" \" and i+1 < len(s) and s[i+1] != \" \":  # 注意考虑 s5 的情况\n",
    "        #         count += 1\n",
    "        # return count\n",
    "\n",
    "        # 法二：\n",
    "        _ = s.split(\" \")\n",
    "        count = 0\n",
    "        for string in _:\n",
    "            if string != \"\":\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        word = s.split()\n",
    "        return len(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if not str:\n",
    "            return 0\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len([ss for ss in s.split(\" \") if ss != \"\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        l = s.split()\n",
    "        return len(l)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if not s:\n",
    "            return 0\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return sum(st != \"\" for st in s.split(\" \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if s==\"\":\n",
    "            return 0\n",
    "        lis=s.split(\" \")\n",
    "        cnt=0\n",
    "        for i in lis:\n",
    "            if i !=\"\":\n",
    "                cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def countSegments(self, s):\n",
    "        return len(s.split())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        SplitStr = s.split()\n",
    "        return len(SplitStr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        if s.isspace() or not s:\n",
    "            return 0\n",
    "        count = 0\n",
    "\n",
    "        # for i in s:\n",
    "        #     if i == ', ' or i == ' ':\n",
    "        #         count += 1\n",
    "                \n",
    "        # print(count)\n",
    "        # return count + 1\n",
    "\n",
    "\n",
    "\n",
    "        # 错的的解答\n",
    "        new = s.split()\n",
    "        print(len(new))\n",
    "        return len(new)\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def countSegments(self, s: str) -> int:\r\n",
    "        # return len(s.strip().split())\r\n",
    "        segment_count = 0\r\n",
    "\r\n",
    "        for i in range(len(s)):\r\n",
    "            if (i == 0 or s[i - 1] == ' ') and s[i] != ' ':\r\n",
    "                segment_count += 1\r\n",
    "\r\n",
    "        return segment_count\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return (len(s.split()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countSegments(self, s: str) -> int:\n",
    "        return len(s.split())\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
